package palmerpuric.tweets2bits;

import flash.display.Bitmap;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.Stage;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.AntiAliasType;
import flash.display.SimpleButton;
import flash.display.GradientType;
import flash.display.SpreadMethod;
import flash.geom.Matrix;
import flash.Lib;
import flash.text.TextFormat;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.utils.IExternalizable;
import twitter.api.TwitterSearch;
import twitter.api.Twitter;
import flash.display.BitmapData;

class Pipes extends BitmapData { public function new() { super(0, 0); } }
class ResultBar extends BitmapData { public function new() { super(0, 0); } }
class SearchBar extends BitmapData { public function new() { super(0, 0); } }
class SearchButton extends BitmapData { public function new() { super(0, 0); } }
class SearchButtonDown extends BitmapData { public function new() { super(0, 0); } }
class SuckingMachinery extends BitmapData { public function new() { super(0, 0); } }


/**
 * Shredder (ne, ne tisti iz Nidja zelv) skrbi za prikazovanje vsega mambodzamba,
 * ki ga je zakuhal Puric (ZV).
 * @author Palmer
 */

class Shredder
{
	// Konstante.	
	public static var SEARCHBOX_BACKGROUND_OFFSET_TOP : Int = 80;
	public static var SEARCHBOX_BACKGROUND_OFFSET_LEFT : Int = 21;
	public static var SEARCHBOX_OFFSET_TOP : Int = 78;
	public static var SEARCHBOX_OFFSET_LEFT : Int = 268;	
	public static var SEARCHBOX_LENGTH : Int = 455;
	public static var SEARCHBOX_MAXCHARS : Int = 63;
	public static var BUTTON_OFFSET_TOP : Int = 80;
	public static var BUTTON_OFFST_LEFT : Int = 728;
	public static var RESULTBOX_BACKGROUND_OFFSET_TOP : Int = 150;
	public static var RESULTBOX_BACKGROUND_OFFSET_LEFT : Int = 21;
	public static var RESULTBOX_OFFSET_TOP : Int = 90;
	public static var RESULTBOX_OFFSET_LEFT : Int = 20;
	public static var RESULTBOX_LENGTH : Int = 981;
	public static var TWEET_MAXCHARS : Int = 140;
	public static var PIPES_BACKGROUND_OFFSET_TOP : Int = 120;
	public static var PIPES_BACKGROUND_OFFSET_LEFT : Int = 21;
	public static var SUCKINGMACHINERY_BACKGROUND_OFFSET_TOP : Int = 362;
	public static var SUCKINGMACHINERY_BACKGROUND_OFFSET_LEFT : Int = 21;
	public static var CAT_X_POS : Int = 505;
	public static var CAT_Y_POS : Int = 575;
	public static var DROP_X_POS : Int = 530;
	public static var DROP_Y_POS : Int = 575;	
		
	// Fieldi.
	private var _mc : MovieClip;
	private var _searchBox : TextField;	
	private var _searchButton : SimpleButton;
	private var _resultBox : TextField;
	private var _catEating : MovieClip;
	private var _catWaiting : MovieClip;
	private var _catTransition : MovieClip;
	private var _drop : MovieClip;
	
	public function new() {
		_mc = new flash.display.MovieClip();
		_mc.addEventListener(Event.ENTER_FRAME, onEnterFrame);
		flash.Lib.current.addChild(_mc);
		
		initBackground();
		initGui();
		initAnims();
	}
	
	function initBackground() {
		makeSearchBarBackground();
		makeResultBar();
		makePipes();
		makeSuckingMachinery();
	}
	
	function makeSearchBarBackground() {		
		var searchBarBitmap : Bitmap = new Bitmap(new SearchBar());
		searchBarBitmap.x = SEARCHBOX_BACKGROUND_OFFSET_LEFT;
		searchBarBitmap.y = SEARCHBOX_BACKGROUND_OFFSET_TOP;		
		Lib.current.addChild(searchBarBitmap);			
	}
	
	function makeResultBar() {
		var searchResultBarBitmap : Bitmap = new Bitmap(new ResultBar());
		searchResultBarBitmap.x = RESULTBOX_BACKGROUND_OFFSET_LEFT;
		searchResultBarBitmap.y = 109;		
		Lib.current.addChild(searchResultBarBitmap);					
	}
	
	function makePipes() {
		var pipesBitmap : Bitmap = new Bitmap(new Pipes());
		pipesBitmap.x = PIPES_BACKGROUND_OFFSET_LEFT;
		pipesBitmap.y = PIPES_BACKGROUND_OFFSET_TOP;
		Lib.current.addChild(pipesBitmap);
	}
	
	function makeSuckingMachinery() {
		var machineBitmap : Bitmap = new Bitmap(new SuckingMachinery());
		machineBitmap.x = SUCKINGMACHINERY_BACKGROUND_OFFSET_LEFT;
		machineBitmap.y = SUCKINGMACHINERY_BACKGROUND_OFFSET_TOP;
		Lib.current.addChild(machineBitmap);
	}
	
	function initGui() {
		initSearchBox();		
		initSearchButton();
		initResultBox();
	}
		
	function initSearchBox() {
		_searchBox = new TextField();
		var format:TextFormat = new TextFormat();
		format.font = "Courier New";		
		format.color = 0x000000;
		format.size = 11;
		
		_searchBox.x = SEARCHBOX_OFFSET_LEFT;
		_searchBox.y = SEARCHBOX_OFFSET_TOP;
		_searchBox.type = TextFieldType.INPUT;
		_searchBox.defaultTextFormat = format;
		_searchBox.width = SEARCHBOX_LENGTH;
		_searchBox.height = 18;
		_searchBox.maxChars = SEARCHBOX_MAXCHARS;
		
		_searchBox.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
		Lib.current.addChild(_searchBox);
	}
	
	function initResultBox() {
		_resultBox = new TextField();
		var format:TextFormat = new TextFormat();
		format.font = "Courier New";		
		format.color = 0xFFFFFF;
		format.size = 11;
		
		_resultBox.x = RESULTBOX_OFFSET_LEFT;
		_resultBox.y = 104;		
		_resultBox.defaultTextFormat = format;
		_resultBox.width = RESULTBOX_LENGTH;
		_resultBox.height = 18;
		_resultBox.maxChars = TWEET_MAXCHARS;
		
		Lib.current.addChild(_resultBox);
	}
	
	function initSearchButton() {
		var buttonSprite: Sprite = new Sprite();
		buttonSprite.graphics.beginBitmapFill(new SearchButton());
		buttonSprite.graphics.drawRect(0, 0, 42, 15);
		buttonSprite.graphics.endFill();
		
		var buttonDownSprite: Sprite = new Sprite();
		buttonDownSprite.graphics.beginBitmapFill(new SearchButtonDown());
		buttonDownSprite.graphics.drawRect(0, 0, 42, 15);
		buttonDownSprite.graphics.endFill();

		
		_searchButton = new SimpleButton();
		_searchButton.x= BUTTON_OFFST_LEFT;
		_searchButton.y = BUTTON_OFFSET_TOP;						
		_searchButton.upState = buttonSprite;
		_searchButton.overState = buttonDownSprite;
		_searchButton.downState = buttonDownSprite;
		_searchButton.hitTestState = _searchButton.upState;
		
		Lib.current.addChild(_searchButton);
		_searchButton.addEventListener(MouseEvent.CLICK, onSearchButtonClick);		
	}
	
	function initAnims() {			
		
		_catEating = Lib.attach("cat_nomnom");
		_catEating.x = CAT_X_POS;
		_catEating.y = CAT_Y_POS;
		flash.Lib.current.addChild(_catEating);
		_catEating.visible = false;
					
		_catWaiting = Lib.attach("cat_looking");
		_catWaiting.x = CAT_X_POS;
		_catWaiting.y = CAT_Y_POS;
		flash.Lib.current.addChild(_catWaiting);
		_catWaiting.visible = true;
	
		_drop = Lib.attach("drop");
		_drop.x = DROP_X_POS;
		_drop.y = DROP_Y_POS;
		flash.Lib.current.addChild(_drop);
		_drop.visible =false;				
	}
	
	function onSearchButtonClick(m:MouseEvent):Void {
		if (isNewSearchAllowed())
			doSearch();
		else if (_searchBox.text.length == 0)
			_searchBox.text = "Missing keyword!";
		
		// TEST
		/*
		var picBitsMaker : PicBitsMaker = new PicBitsMaker(_resultBox);
		picBitsMaker.doMagic("papapapcupanekajlepega_test hehe", "test"); //tweet, keyword
		*/
	}
	
	function onKeyDown(event:KeyboardEvent) : Void {
		if (event.keyCode == 13) { // keycode 13 = ENTER
			if (isNewSearchAllowed())
				doSearch();
			else if (_searchBox.text.length == 0)
				_searchBox.text = "Missing keyword!";
		}
	}
	
	// nov iskanje je dovoljeno ce imamo keyword in so biti prejsnjega iskanja ze izginili
	function isNewSearchAllowed() : Bool {
		if (_searchBox.text.length > 0 && Lib.current.numChildren < 12)
			return true;
		else
			return false;
	}
	
	function doSearch() : Void {
		var tweetsFinder : TweetsFinder = new TweetsFinder(_resultBox);
		tweetsFinder.search(_searchBox.text);
	}
	
	function onEnterFrame(event:Event) : Void {
		// 10 childov se doda v Shredder classu - testiral z izpisovanjem Lib.current.numChildren
		if (Lib.current.numChildren < 12) {
			// Nehaj se fojtrat.
			_catEating.visible = false;
			_catWaiting.visible = true;
			_drop.visible = false;
		} else if (PicBit.firstDeath) {
			// Fojtraj se.
			_catEating.visible = true;
			_catWaiting.visible = false;
			_drop.visible = true;
		}		
	}
	
	static function main() {	
		new Shredder();
	}
}
